package ThreadDemo;

public class ThreadDemo8 {
    public static void main(String[] args) throws InterruptedException {
        Object locker1=new Object();
        Object locker2=new Object();
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                while (!Thread.currentThread().interrupted()) {
                    synchronized (locker1.getClass()) {
                        for (int i = 0; i < 1; i++) {
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                                break;
                            }
                            System.out.println("这是线程1");
                        }
                    }
                }
            }
        });
        thread.start();
        Thread thread2=new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (locker2.getClass()) {

                    for (int i = 0; i < 5; i++) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("这是线程2");
                    }
                }
            }
        });
        thread2.start();
        Thread.sleep(700);
        thread.interrupt();
    }
}
